﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Timers;
using Cinemachine;
using UnityEngine;
using UnityEngine.Playables;
using XLua;
using Yoozoo.Managers.ResourceManagerV2.Runtime;
using Yoozoo.Mars.Got;

public struct CarViewParams
{
    public string timelinePath;
    public GameObject root;
    public CarTimelineSystem system;
}
[LuaCallCSharp]
public class CarTimelineController : MonoBehaviour
{
    private ResLoader m_resLoader;
    private List<CarViewParams> carTimelineView;
    public CinemachineBrain camera;

    [LuaCallCSharp]
    public void Initialize(List<string> carTimeline)
    {
        m_resLoader = ResLoader.Alloc();
        carTimelineView = new List<CarViewParams>(5);
        for (int i = 0; i < carTimeline.Count; ++i)
        {
            carTimelineView.Add(GetOriginCarViewParams(carTimeline[i]));
        }
        //carTimelineView.Add(GetOriginCarViewParams("Assets/ResourcesAssets/Prefabs/Character/Car/CameraControl/TimelineActivate.prefab"));
    }

    [LuaCallCSharp]
    public void LoadViewController(int viewType,Action callback)
    {
        if (carTimelineView.Count >= viewType+1)
        {
            if (carTimelineView[viewType].root != null)
            {
                if (carTimelineView[viewType].system!=null)
                {
                    carTimelineView[viewType].system.Start();
                    double timer = carTimelineView[viewType].system.cTrack.duration;
                    Timers.inst.Add((float)timer,1, (o) =>
                    {
                        callback?.Invoke();
                    }); 
                }
                else
                {
                    callback?.Invoke();
                }
            }
            else
            {
                //root为空,需要加载
                m_resLoader.Add2LoadRapid(carTimelineView[viewType].timelinePath, (suc, str, obj) =>
                {
                    if (suc)
                    {
                        GameObject _obj = obj as GameObject;
                        
                        CarViewParams cvp = carTimelineView[viewType];
                        cvp.root = Instantiate(_obj, transform);
                        cvp.system = cvp.root.GetComponent<CarTimelineSystem>();
                        cvp.system.SetCamera(camera);
                        carTimelineView[viewType] = cvp;
                        if (cvp.system)
                        {
                            cvp.system.Start();
                            double timer = carTimelineView[viewType].system.cTrack.duration;
                            Timers.inst.Add((float)timer,1, (o) =>
                            {
                                callback?.Invoke();
                            }); 
                        }
                        else
                        {
                            callback?.Invoke();
                        }
                    }
                    else
                    {
                        callback?.Invoke();
                    }
                });
                m_resLoader.Load();
            }
        }
        //没有,不播放
    }
    [LuaCallCSharp]
    public void StopViewController(int viewType)
    {
        if (carTimelineView.Count >= viewType+1)
        {
            if (carTimelineView[viewType].system != null)
            {
                carTimelineView[viewType].system.Stop();
            }
        }
    }

    private CarViewParams GetOriginCarViewParams(string path)
    {
        CarViewParams cvp = new CarViewParams();
        cvp.timelinePath = path;
        return cvp;
    }

    private void OnDestroy()
    {
        m_resLoader?.ReleaseAllRes();
        m_resLoader = null;
    }
}
